package battleship;
import java.io.*;
import java.util.Scanner;
import javax.swing.JOptionPane;
/**
 *
 * @author Code and Psuedocode: Shaun Pickford
 * This class will create and return a character array of 100 characters or null value
 * that indiciate if there is a ship in the array position or not.
 *
 * A = Aircraft Carrier = 5 hits
 * B = Battleship = 4 hits
 * C = Carrier = 3 hits
 * S = Submarine = 3 hits
 * D = Destroyer = 2 hitS
 */
public class OpponentGameBoard{

    File game = new File("Game1.txt");
    final int GRID_SIZE = 100;
    char[] gameBoard = new char[GRID_SIZE];
    boolean[] shotRecord = new boolean[GRID_SIZE];
    boolean boardCreated = false;

    /* No-arg constructor */
    public void OpponentGameBoard(){
        
    }//OpponentGameBoard

    /* This method creates the game board...should only be called once per game */
    public void createGameBoard(){

        if(!boardCreated)
        {
            int index = 0;
            try{
                Scanner scanner = new Scanner(game);
                    while(index < 100){
                    while(scanner.hasNextLine()){
                    String temp;
                    char temp2;
                    temp = scanner.nextLine();
                    temp2 = temp.charAt(0);
                    gameBoard[index] = temp2;
                    index++;
                    boardCreated = true;
                    }//while scanner.hasNextLine
                    }//while index < 100
                }//try

            catch (FileNotFoundException e){
                e.printStackTrace();
            }
        }//end if boardCreated = false
        else
        {
            JOptionPane.showMessageDialog(null, "Opponent Game Board Already Created!");
        }
    }

    public char[] getGameBoard(){

        return gameBoard;
    }

    public void showGameBoard(){

        int index = 0;
        while(index < 100){
            System.out.println("Position " + index + ": " + gameBoard[index]);
            index++;
        }
    }

    public String hitOrMiss(int coordinate){

        if (!shotRecord[coordinate - 1])
        {

        boolean hOrM = false;
        String ship = "";

        if (gameBoard[coordinate - 1] == 'N')
        {
            hOrM = false;
            shotRecord[coordinate - 1] = true;
        }

        else if (gameBoard[coordinate - 1] == 'D')
        {
            hOrM = true;
            ship = "Destroyer";
            shotRecord[coordinate - 1] = true;
        }

        else if (gameBoard[coordinate - 1] == 'C')
        {
            hOrM = true;
            ship = "Cruiser";
            shotRecord[coordinate - 1] = true;
        }

        else if (gameBoard[coordinate - 1] == 'S')
        {
            hOrM = true;
            ship = "Submarine";
            shotRecord[coordinate - 1] = true;
        }

        else if (gameBoard[coordinate - 1] == 'B')
        {
            hOrM = true;
            ship = "Battleship";
            shotRecord[coordinate - 1] = true;
        }

        else if (gameBoard[coordinate - 1] == 'A')
        {
            hOrM = true;
            ship = "Aircraft Carrier";
            shotRecord[coordinate - 1] = true;
        }

        if (hOrM)
        {
           return ship;
        }
        else
        {
           return "miss";
        }
        }//if the shot hasnt been tried already
        else
        {
            JOptionPane.showMessageDialog(null, "You have already tried that coordinate!");
            return null;
        }
    }

    public boolean[] getShotRecord(){

        return shotRecord;

    }


}
